﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace CMSLibrary.TagsCreate.CustomFunction.Common
{
    /// <summary>
    /// 日期间隔
    /// 1.  DateDiff(StartDate)  开始日期，计算单位（默认：天）
    /// 2.  DateDiff(StartDate,EndDate)  ，开始日期，结束日期
    /// 3.  DateDiff(datepart,StartDate)  计算单位，开始日期
    /// 4.  DateDiff(datepart,StartDate,EndDate)  计算单位，开始日期，结束日期    
    /// </summary>
    public class Custom_DateDiff : PFunctionBase
    {
        #region 参数成员
        /// <summary>
        /// 开始日期
        /// </summary>        
        DateTime sdate = DateTime.Now;
        /// <summary>
        /// 结束日期
        /// </summary>        
        DateTime edate = DateTime.Now;
        /// <summary>
        /// 计算单位
        /// </summary>        
        string datepart = "DAY";
        #endregion

        #region 重写的方法
        protected override void SplitFormatParas(string sourceParas)
        {
            string[] paras = GetParasArray(sourceParas);
            //确定重载方法,由多到少 
            /********** 4 **********/
            try
            {
                datepart = Convert.ToString(paras[0]);
                sdate = Convert.ToDateTime(paras[1]);
                edate = Convert.ToDateTime(paras[2]);
                FunctionBurden = BurdenIndex.four;
                return;
            }
            catch { }
            /********** 3 **********/
            try
            {
                datepart = Convert.ToString(paras[0]);
                sdate = Convert.ToDateTime(paras[1]);                
                FunctionBurden = BurdenIndex.three;
                return;
            }
            catch { }
            /********** 2 **********/
            try
            {
                datepart = "DAY";
                sdate = Convert.ToDateTime(paras[0]);
                edate = Convert.ToDateTime(paras[1]);
                FunctionBurden = BurdenIndex.two;
                return;
            }
            catch { }
            /********** 1 **********/
            try
            {
                datepart = "DAY";
                sdate = Convert.ToDateTime(paras[0]);
                FunctionBurden = BurdenIndex.one;
                return;
            }
            catch { new Exception(); }
        }

        public override string ToString()
        {
            string result = string.Empty;
            result = DateDiff(datepart, sdate, edate).ToString();
            return result;
        }
        #endregion

        /// <summary>
        /// 日期间隔
        /// </summary>
        /// <param name="datepart">间隔单位(month,day,hour,minute,second)</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>间隔整数</returns>
        public static int DateDiff(string datepart, DateTime startDate, DateTime endDate)
        {
            Double result = 0;
            TimeSpan ts = startDate - endDate;
            switch (datepart.ToUpper())
            {
                case "MONTH":
                    result = ts.Days / 30;
                    break;
                case "DAY":
                    result = ts.Days;
                    break;
                case "HOUR":
                    result = ts.Hours;
                    break;
                case "MINUTE":
                    result = ts.Minutes;
                    break;
                case "SECOND":
                    result = ts.Seconds;
                    break;
            }
            return (int)Math.Floor(result);
        }
    }
}
